"""
NC120 二进制中1的个数
https://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8?tpId=117&&tqId=37771&&companyId=239&rp=1&ru=/company/home/code/239&qru=/ta/job-code-high/question-ranking
"""
#
# 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
#
#
# @param n int整型
# @return int整型
#


class Solution:

    def _ten2bin(self, num: int) -> str:
        """
        对于
        10进制转化为2进制
        """
        assert num > 0, "num must greater than zero."

        if num <= 1:
            return str(num).rjust(32, '0')

        tmp_lst = []
        while num:
            num, mod = divmod(num, 2)
            tmp_lst.append(str(mod))

        tmp_lst.reverse()
        return "".join(tmp_lst).rjust(32, '0')

    # def method1(self, num: int) -> int:
    #     # num>=0，统计1的个数
    #     if num >= 0:
    #         tmp = self._ten2bin(num)
    #         print(tmp)
    #         return tmp.count('1')
    #     else:
    #         # FIXME 这里不对，需要了解二进制补码
    #         # num <0, 统计0的个数，用32-0
    #         tmp = self._ten2bin(-num)
    #         print(tmp)
    #         return 32 - tmp.count('0')

    def method2(self, num: int) -> int:
        """
        题解：按位比较
        """
        res = 0
        for i in range(32):
            if num & (1 << i):
                res += 1
        return res

    def NumberOf1(self, n: int) -> int:
        # write code here
        return self.method2(n)


def test():
    obj = Solution()
    num = -1
    res = obj.NumberOf1(num)
    print(res)


if __name__ == "__main__":
    test()
